// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_MODE, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from "./../StaticInstance";
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import DataManager from "../manager/DataManager";
import { ReactiveAutoSave } from "../Tool/ReactiveAutoSave";
import { BytePlantform } from "../Tool/BytePlantform";
import { slider_page } from "../Tool/slider_page";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MenuLayer extends BaseLayer {
  btnStart: cc.Node = null;
  btnSetting: cc.Node = null;
  btnGames: cc.Node = null;
  btnShare: cc.Node = null;
  btnRank: cc.Node = null;
  btnGift: cc.Node = null;
  currentLevel: cc.Node = null;
  btnStartPk: cc.Node = null;

  onLoad() {
    console.log("MenuLayer onLoad");
    this.btnStart = cc.find("main/btn_start", this.node);
    this.btnSetting = cc.find("btn_setting", this.node);
    this.btnGames = cc.find("bottom/btn_games", this.node);
    this.btnShare = cc.find("bottom/btn_share", this.node);
    this.btnRank = cc.find("bottom/btn_rank", this.node);
    this.btnGift = cc.find("bottom/gift", this.node);
    this.btnStart.on("click", this.onStartClick, this);
    this.btnSetting.on("click", this.onSettingClick, this);
    this.btnGames.on("click", this.onGamesClick, this);
    this.btnShare.on("click", this.onShareClick, this);
    this.btnRank.on("click", this.onRankClick, this);
    this.btnGift.on("click", this.onGiftClick, this);
    this.currentLevel = cc.find("main/btn_start/current_level/num", this.node);
    this.btnStartPk = cc.find("main/btn_start_pk", this.node);
    this.btnStartPk.on("click", this.onStartPkClick, this);
    ReactiveAutoSave.init("gameData", { gift: 0, level: 1 });
    this.check_douyin();
  }

  onDestroy() {
    this.btnStart.off("click", this.onStartClick, this);
    this.btnSetting.off("click", this.onSettingClick, this);
    this.btnGames.off("click", this.onGamesClick, this);
    this.btnShare.off("click", this.onShareClick, this);
    this.btnRank.off("click", this.onRankClick, this);
    this.btnStartPk.off("click", this.onStartPkClick, this);
  }

  onEnable() {
    this.rendorPower();
    this.rendorPowerTimer();
    this.rendor();
  }

  onDisable() {}

  rendor() {
    this.currentLevel.getComponent(
      cc.Label
    ).string = `${DataManager.instance.level}关`;
  }

  check_douyin() {
    //奖励按钮隐藏
    this.show_gift_btn();
    let self = this;
    BytePlantform.ins.check_side_bar((success: boolean) => {
      if (success) {
        console.log("check_douyin 检查侧边栏 success,可以使用侧边栏");
        self.listenFromDouyi();
      } else {
        console.log("check_douyin 跳转失败 fail,结束判断");
      }
    });
  }

  show_gift_btn() {
    this.btnGift.active = false;
    let self = this;
    if (ReactiveAutoSave.ins.getData().gift >= 2) {
      this.btnGift.active = false;
      console.log("show_gift_btn 领取过奖励 不显示 ");
    } else {
      //没领取过奖励
      BytePlantform.ins.check_side_bar((success: boolean) => {
        if (success) {
          console.log("show_gift_btn 检查侧边栏 success显示 ");
          self.btnGift.active = true;
        } else {
          console.log("show_gift_btn 检查侧边栏  fail ");
          self.btnGift.active = false;
        }
      });
    }
  }
  private listenFromDouyi() {
    if (ReactiveAutoSave.ins.getData().gift == 2) {
      //0 =default,1=添加了。2=领取奖励
      console.log("check_douyin2 侧边栏奖励领取过，直接返回");
      return;
    }
    let self = this;
    BytePlantform.ins.listen_douyin((success: boolean) => {
      if (success) {
        //TODO 在这里完成你的奖励逻辑
        console.log(
          "main 从侧边栏调过来，可以奖励用户了！" +
            ReactiveAutoSave.ins.getData().gift
        );
        if (ReactiveAutoSave.ins.getData().gift < 1) {
          ReactiveAutoSave.ins.getData().gift = 1;
          console.log("add_siderbar 添加侧边栏跳转成功,记录数据 1");
          //更新页面
        //   console.log("单例" + slider_page);
          slider_page.ins.refrush_data();
        }
      } else {
        console.log("main 不是从侧边栏调过来");
      }
    });
  }

  async onStartClick() {
    AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK);
    if (DataManager.instance.power <= 0) {
      ToastManager.instance.show("红心已用完, 请补充红心", {
        gravity: "TOP",
        bg_color: cc.color(226, 69, 109, 255),
      });
    } else {
      DataManager.instance.mode = ENUM_GAME_MODE.NORMAL;
      DataManager.instance.power -= 1;
      DataManager.instance.save();
      await StaticInstance.fadeManager.fadeIn();
      StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false);
      StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN);
      StaticInstance.gameManager.onGameStart();
    }
  }

  onStartPkClick() {
    AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK);
    if (DataManager.instance.power <= 0) {
      ToastManager.instance.show("红心已用完, 请补充红心", {
        gravity: "TOP",
        bg_color: cc.color(226, 69, 109, 255),
      });
    } else {
      DataManager.instance.mode = ENUM_GAME_MODE.PK;
      DataManager.instance.power -= 1;
      DataManager.instance.save();
      StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false);
      StaticInstance.uiManager.toggle(ENUM_UI_TYPE.READY);
    }
  }

  onSettingClick() {
    AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK);
    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false);
    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SETTING);
  }

  onGamesClick() {
    AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK);
    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false);
    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MORE);
  }

  onShareClick() {
    AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK);
    if (SdkManager.instance.getPlatform()) {
      SdkManager.instance.activeShare();
    } else {
      ToastManager.instance.show("仅支持小游戏平台", {
        gravity: "TOP",
        bg_color: cc.color(226, 69, 109, 255),
      });
    }
  }

  onRankClick() {
    AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK);
    ToastManager.instance.show("正在开发中~", {
      gravity: "CENTER",
      bg_color: cc.color(76, 76, 76, 255),
    });
    // StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
    // StaticInstance.uiManager.toggle(ENUM_UI_TYPE.RANK)
  }

  onGiftClick() {
    AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK);
    // StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.GIFT);
  }
}
